package com.sig.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 校验合法性工具
 *
 * @author sig
 * @since 2019/1/26
 */
public final class ValidatorUtil {
    /** 阿拉伯数字下限(包含) */
    private static final int NUMBER_MIN = 0x0030;
    /** 阿拉伯数字上限(包含) */
    private static final int NUMBER_MAX = 0x0039;
    /** 大写字母下限(包含) */
    private static final int CAPITAL_LETTER_MIN = 0x0041;
    /** 大写字母上限(包含) */
    private static final int CAPITAL_LETTER_MAX = 0x005A;
    /** 小写字母下限(包含) */
    private static final int SMALL_LETTER_MIN = 0x0061;
    /** 小写字母上限(包含) */
    private static final int SMALL_LETTER_MAX = 0x007A;
    /** 中文下限(包含) */
    private static final int CHINESE_MIN = 0x4E00;
    /** 中文上限(包含) */
    private static final int CHINESE_MAX = 0x9FFF;
    /** 表情符号正则匹配 */
    private static final Pattern EMOTICON_PATTERN = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
    /** 数字字母开头正则匹配 */
    private static final Pattern NUMBER_LETTER_BEGIN_PATTERN = Pattern.compile("^\\w.*$");
    /** 用户名正则匹配 */
    private static final Pattern USERNAME_PATTERN = Pattern.compile("^[a-zA-Z]\\w{5,17}$");
    /** 密码正则匹配 */
    private static final Pattern PASSWORD_PATTERN = Pattern.compile("^\\w{6,16}$");
    /** 手机号码正则匹配 */
    private static final Pattern MOBILE_PATTERN = Pattern.compile("^((17[0-9])|(13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
    /** 邮箱正则匹配 */
    private static final Pattern EMAIL_PATTERN = Pattern.compile("^(\\w+[-|.]?)+\\w@(\\w+(-\\w+)?\\.)+[a-zA-Z]{2,}$");
    /** 身份证号码正则匹配 */
    private static final Pattern ID_CARD_PATTERN = Pattern.compile("(^[A-Z0-9]{18}$)|(^\\d{15}$)");
    /** URL正则匹配 */
    private static final Pattern URL_PATTERN = Pattern.compile("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");
    /** IP地址正则匹配 */
    private static final Pattern IP_ADDRESS_PATTERN = Pattern.compile("(25[0-5])|(2[0-4]\\d)|([0-1]\\d{2})|([1-9]?\\d)");
    /** 纯数字正则匹配 */
    private static final Pattern PURE_NUMBER_PATTERN = Pattern.compile("^\\d+$");
    /** 纯字母正则匹配 */
    private static final Pattern PURE_LETTER_PATTERN = Pattern.compile("^[a-zA-Z]+$");
    /** 纯中文正则匹配 */
    private static final Pattern PURE_CHINESE_PATTERN = Pattern.compile("^[\u4e00-\u9fa5]+$");

    private ValidatorUtil() {
    }

    /**
     * 检查长度
     *
     * @param content
     * @param minLen
     * @param maxLen
     */
    public static void checkLength(final String content, final int minLen, final int maxLen) {
        checkLength(content, minLen, maxLen, false);
    }

    /**
     * 检查长度
     *
     * @param content
     * @param minLen
     * @param maxLen
     * @param charMark true：按字符计算；false：按字节计算
     */
    public static void checkLength(final String content, final int minLen, final int maxLen, final boolean charMark) {
        final int len = getLength(content, charMark);
        if (len < minLen || len > maxLen)
            throw new RuntimeException();
    }

    /**
     * 获取长度
     *
     * @param content
     * @return
     */
    public static int getLength(final String content) {
        return getLength(content, false);
    }

    /**
     * 获取长度
     *
     * @param content
     * @param charMark true：按字符计算；false：按字节计算
     * @return
     */
    public static int getLength(final String content, final boolean charMark) {
        int result = 0;
        if (content != null && !content.isEmpty()) {
            if (charMark)
                return content.length();
            for (int index = 0, len = content.length(); index < len; index++) {
                final char charAt = content.charAt(index);
                if (charAt >= CHINESE_MAX) {
                    result += 2;
                    continue;
                }
                result++;
            }
        }
        return result;
    }

    /**
     * 是否包含不允许的字符
     *
     * @param content
     * @return
     */
    public static boolean containsIllegalChars(final String content) {
        if (content == null || content.trim().isEmpty())
            return true;
        for (int index = 0, len = content.length(); index < len; index++) {
            final char charAt = content.charAt(index);
            if ((charAt >= NUMBER_MIN && charAt <= NUMBER_MAX) ||
                    (charAt >= CAPITAL_LETTER_MIN && charAt <= CAPITAL_LETTER_MAX) ||
                    (charAt >= SMALL_LETTER_MIN && charAt <= SMALL_LETTER_MAX) ||
                    (charAt >= CHINESE_MIN && charAt <= CHINESE_MAX) ||
                    (charAt == '.' || charAt == '-'))
                continue;
            return true;
        }
        return false;
    }

    /**
     * 是否包含表情符号
     *
     * @param content
     * @return
     */
    public static boolean containsEmoticonChars(final String content) {
        final Matcher matcher = EMOTICON_PATTERN.matcher(content);
        return matcher.find();
    }

    /**
     * 检查是否以数字或字母开头
     *
     * @param content
     * @return
     */
    public static boolean checkBeginChars(final String content) {
        final Matcher matcher = NUMBER_LETTER_BEGIN_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查用户名是否合法
     *
     * @param content
     * @return
     */
    public static boolean checkUserName(final String content) {
        final Matcher matcher = USERNAME_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查密码是否合法
     *
     * @param content
     * @return
     */
    public static boolean checkPassword(final String content) {
        final Matcher matcher = PASSWORD_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查手机号码是否合法
     *
     * @param content
     * @return
     */
    public static boolean checkMobile(final String content) {
        final Matcher matcher = MOBILE_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查邮箱是否合法
     *
     * @param content
     * @return
     */
    public static boolean checkEmail(final String content) {
        final Matcher matcher = EMAIL_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查身份证号码是否合法
     *
     * @param content
     * @return
     */
    public static boolean checkIdCard(final String content) {
        final Matcher matcher = ID_CARD_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查URL是否合法
     *
     * @param content
     * @return
     */
    public static boolean checkUrl(final String content) {
        final Matcher matcher = URL_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查IP地址是否合法
     *
     * @param content
     * @return
     */
    public static boolean checkIpAddress(final String content) {
        final Matcher matcher = IP_ADDRESS_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查是否纯数字
     *
     * @param content
     * @return
     */
    public static boolean checkPureNumber(final String content) {
        final Matcher matcher = PURE_NUMBER_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查是否纯字母
     *
     * @param content
     * @return
     */
    public static boolean checkPureLetter(final String content) {
        final Matcher matcher = PURE_LETTER_PATTERN.matcher(content);
        return matcher.matches();
    }

    /**
     * 检查是否纯中文
     *
     * @param content
     * @return
     */
    public static boolean checkPureChinese(final String content) {
        final Matcher matcher = PURE_CHINESE_PATTERN.matcher(content);
        return matcher.matches();
    }
}
